<?php

namespace app\controller\Api\V1\Client;

use app\helper\GetProductInfo;
use app\model\GameAuthorization;
use app\model\GameMultipleVersion;
use app\model\ToolServer;
use app\request\MultipleVersion;
use Exception;
use Illuminate\Support\Arr;
use support\Request;
use support\Response;

class MultipleVersionController
{

    protected GetProductInfo $getProductInfoRe;


    public function __construct()
    {
        $this->getProductInfoRe = GetProductInfo::getInstance();
    }


    /**
     * 获取多版本列表
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function List(Request $request): Response
    {

        $validate = new MultipleVersion();

        if (!$validate->scene('list')->check($request->all())) {
            throw new Exception($validate->getError(), 500);
        }

        if ($request->input('release_type') == 1) {
            $self = GameAuthorization::query()->where('merchant', $request->input('mch_id'))
                ->where('ulid', $request->input('game_id'))->first();
            if (!$self) {
                throw new Exception('游戏不存在', 500);
            }
            $self = $self->toArray();

            $list = GameMultipleVersion::query()->has('Game')->where('merchant', $request->input('mch_id'))
                ->where('game', $request->input('game_id'))->where('state', 1)
                ->get()->map(function ($row) use ($request) {
                    return [
                        'game' => $row['obj'],
                        'title' => $row['alias'] ?? $row->Game->title,
                    ];
                })->toArray();

            $arr = Arr::collapse([[[
                'game' => $self['ulid'],
                'title' => $self['title'],
            ]], $list]);
        } else {
            if (!empty($request->input('mch_id'))) {
                $user = $request->input('mch_id');
                $productInfo = $this->getProductInfoRe->GetMerchantPlatform();
            } elseif (!empty($request->input('open_id'))) {
                $user = $request->input('open_id');
                $productInfo = $this->getProductInfoRe->GetDeveloperPlatform();
            } else {
                throw new Exception('包信息有误', 500);
            }

            if (!empty($request->input('game_id')) && $request->input('game_id') != 0) {
                $toolServerInfo = ToolServer::query()->where('ulid', $request->input('game_id'))->first();

                if ($toolServerInfo) {
                    $toolServerInfo = $toolServerInfo->toArray();

                    $arr = [[
                        'game' => $toolServerInfo['ulid'],
                        'title' => $toolServerInfo['title'],
                    ]];
                } else {
                    $arr = [];
                }
            } else {
                $arr = ToolServer::query()->where('product', $productInfo['ulid'])->where('organization', $user)
                    ->get()->map(function ($row) {
                        return [
                            'game' => $row['ulid'],
                            'title' => $row['title'],
                        ];
                    })->toArray();
            }
        }

        return jsonRe([
            'list' => $arr,
        ]);

    }

}